#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N=2e5+10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
int n, p[40]={3,1,4,1,5,9,2,6,5,3,5, 8,9,7,9,3,2,3,8,4,6, 2,6,4,3,3,8,3,2,7,9, 5,0,2,8,8,4,1};
void solve()
{
	string s;
	cin>>s;
	int ans=0;
	for(int i=0;i<s.size();i++)
	{
		if(p[i]==s[i]-'0') ans++;
		else break;
	}
	cout<<ans<<'\n';

	
}

int main()
{
	close();
	int T; cin>>T;
	while (T--) solve();
	return 0;
}